#include "SpriteList.h"

SpriteList::SpriteList()
{
	head = NULL;
}
SpriteList::SpriteList(Sprite * headSprite)
{
	head = new List<Sprite>(headSprite, NULL);
}
SpriteList::~SpriteList()
{
	List<Sprite> * search = head;
	while(search)
	{
		search = search->Remove();
	}	
	head = NULL;
}
void SpriteList::Cleanup()
{
	List<Sprite> * search = head;
	while(search)
	{
		if(search->payload && search->payload->IsDead())
			search = search->Remove();
		else
			search = search->next;
	}	
}
void SpriteList::Add(Sprite * payload)
{
	head = new List<Sprite>(payload, head);
}
void SpriteList::Draw()
{
	List<Sprite> * search = head;
	while(search)
	{
		if(search->payload)
			search->payload->Draw();
		search = search->next;
	}
}
void SpriteList::Think(double elapsedTime)
{
	List<Sprite> * search = head;
	while(search)
	{
		if(search->payload)
			search->payload->Think(elapsedTime);
		search = search->next;
	}
}
void SpriteList::CheckCollisions(Object * object, SupplyBag * supplyBag)
{
	List<Sprite> * search = head;
	while(search)
	{
		if(search->payload && object->CollideSprite(search->payload))
		{
			//do sprite collision stuff here
			bool remove = true;
			if(supplyBag)
			{
				remove = supplyBag->HandleId(search->payload->spriteId);
			}
			
			if(remove)
			{
				if(search == head)
					head = search->next;
				search = search->Remove();
			}
			else
				search = search->next;
			
		}
		else
			search = search->next;
	}
}
